﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text;
public partial class theme_3_R_Evaluation : PowerCreator.LMS.Web.Base.WebBasePage
{
    public string _contentID = "0";
    public string rqResourceID = "0";
    public string StrEvaluation = "";
    public string StrDivID = "";
    PowerCreator.LMS.Web.Grade.GradeManager op = new PowerCreator.LMS.Web.Grade.GradeManager();
    PowerCreator.LMS.Web.User.User opuser = new PowerCreator.LMS.Web.User.User();
    protected PowerCreator.LMS.Web.Config.WebConfig config = new PowerCreator.LMS.Web.Config.WebConfig();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["ResourceID"]!=null)
            {
                rqResourceID = Request.Params["ResourceID"];
            }
        if (Request.Params["ContentID"] != null)
        {
            _contentID = Request.Params["ContentID"].ToString();
        }

            string search = GetSearch();
            BinderPager(search);
            BinderRPT(search, 1);
            //StrEvaluation = GetEvaluation(rqResourceID);

       
    }

    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        if(rqResourceID!="0")
            sbSearch.Append("FKGradeResourceID=" + rqResourceID + " and (GradeContent is not null) and FKGradeSkillID=0");
        if (_contentID != "0")
            sbSearch.Append("FKGradeContentGuid='" + _contentID + "' and (GradeContent is not null) and FKGradeSkillID=0");

        return sbSearch.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = op.CountGrade(search);
        AspNetPager1.PageSize = 5;
    }
    private void BinderRPT(string search, int pageIndex)
    {
        string Rt = "";
        DataTable dt = op.QueryGrade(search, " GradeCreatetime desc", 5, pageIndex);
        if (rqResourceID != "0")
        {
            dt.Columns.Add("FormatGradeAsfTime");
            dt.Columns.Add("FKGradeUserName");
            dt.Columns.Add("GradeContent_Short");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    dt.Rows[i]["FormatGradeAsfTime"] = PowerCreator.LMS.Common.Utils.TimeUtils.FormatTime(dt.Rows[i]["GradeAsfTime"].ToString());
                    dt.Rows[i]["FKGradeUserName"] = opuser.GetRealNameByUserID(int.Parse(dt.Rows[i]["FKGradeUserID"].ToString()));


                    string Position = dt.Rows[i]["GradeAsfTime"].ToString();
                    StrDivID = StrDivID + Position + ",";
                    Rt = Rt + "<div id=\"" + Position + "\"  style=\"display: none;\">" + dt.Rows[i]["FormatGradeAsfTime"].ToString() + "&nbsp;&nbsp;";
                    Rt = Rt + dt.Rows[i]["GradeContent"].ToString();
                    Rt = Rt + "[" + dt.Rows[i]["FKGradeUserName"].ToString() + config.GetLanagerValueByKey("theme_Theme_AddTo", PowerCreator.LMS.Language.LanagetItemType.Page);
                    Rt = Rt + dt.Rows[i]["GradeCreatetime"].ToString() + "]</div>";

                    dt.Rows[i]["GradeContent_Short"] = PowerCreator.LMS.Common.Utils.StringUtils.Left(dt.Rows[i]["GradeContent"].ToString(), 19);
                }
                if (StrDivID.Length > 0)
                    StrDivID = StrDivID.Substring(0, StrDivID.Length - 1);
            }
        }
        if (_contentID != "0")
        {
            dt.Columns.Add("FKGradeUserName");
            dt.Columns.Add("GradeContent_Short");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //dt.Rows[i]["FormatGradeAsfTime"] = PowerCreator.LMS.Common.Utils.TimeUtils.FormatTime(dt.Rows[i]["GradeAsfTime"].ToString());
                dt.Rows[i]["FKGradeUserName"] = opuser.GetRealNameByUserID(int.Parse(dt.Rows[i]["FKGradeUserID"].ToString()));
                dt.Rows[i]["GradeContent_Short"] = PowerCreator.LMS.Common.Utils.StringUtils.Left(dt.Rows[i]["GradeContent"].ToString(), 12);
            }

        }



        lblCommentCount.Text = AspNetPager1.RecordCount.ToString();
        
        rptCommentList.DataSource = dt;
        rptCommentList.DataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    private string GetEvaluation(string ResouurceID)
    {
        string Rt = "";
        string sql="";
        if (rqResourceID != "0")
            sql = "FKGradeResourceID=" + rqResourceID + " and (GradeContent is not null) and FKGradeSkillID=0";
        if (_contentID != "0")
            sql = "FKGradeContentGuid='" + _contentID + "' and (GradeContent is not null) and FKGradeSkillID=0";
  
        string orderby = " GradeCreatetime desc";
        int count = 0;
        count = op.CountGrade(sql);
        DataTable dt = op.QueryGrade(sql, orderby, count, 1);
        dt.Columns.Add("FormatGradeAsfTime");
        dt.Columns.Add("FKGradeUserName");
        //if (dt.Rows.Count >0)
        //{
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {
        //        Rt = Rt + "new hTimerItem(\"" + dt.Rows[i][2].ToString() + "\",\"" + dt.Rows[i][2].ToString() + "  ";
        //        Rt = Rt + dt.Rows[i][1].ToString();
        //        Rt = Rt + "[" + dt.Rows[i][0].ToString() + "发表于";
        //        Rt = Rt + dt.Rows[i][3].ToString() + "]\"),";
        //    }
        //}
        //if (Rt.Length > 0)
        //    Rt = Rt.Substring(0, Rt.Length - 1);
        if (dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["FormatGradeAsfTime"] = PowerCreator.LMS.Common.Utils.TimeUtils.FormatTime(dt.Rows[i]["GradeAsfTime"].ToString());
                dt.Rows[i]["FKGradeUserName"] = opuser.GetRealNameByUserID(int.Parse(dt.Rows[i]["FKGradeUserID"].ToString()));


                string Position = dt.Rows[i]["GradeAsfTime"].ToString();
                StrDivID = StrDivID + Position + ",";
                Rt = Rt + "<div id=\"" + Position + "\"  style=\"display: none;\">" + dt.Rows[i]["FormatGradeAsfTime"].ToString() + "&nbsp;&nbsp;";
                Rt = Rt + dt.Rows[i]["GradeContent"].ToString();
                Rt = Rt + "[" + dt.Rows[i]["FKGradeUserName"].ToString() + config.GetLanagerValueByKey("theme_Theme_AddTo", PowerCreator.LMS.Language.LanagetItemType.Page);
                Rt = Rt + dt.Rows[i]["GradeCreatetime"].ToString() + "]</div>";
            }
            if (StrDivID.Length > 0)
                StrDivID = StrDivID.Substring(0, StrDivID.Length - 1);
        }


        return Rt;
    }
    ///// <summary>
    ///// 绑定主评论信息
    ///// </summary>
    //private void BinderComment()
    //{
    //    PowerCreator.LMS.Web.Grade.GradeManager op = new PowerCreator.LMS.Web.Grade.GradeManager();
    //    PowerCreator.LMS.Web.User.User opuser = new PowerCreator.LMS.Web.User.User();
    //    string sql = "FKGradeResourceID=" + _ResourceID + " and (GradeContent is not null) and FKGradeSkillID=0 ";
    //    string orderby = " GradeAsfTime asc";
    //    int count = 0;
    //    count = op.CountGrade(sql);
    //    DataTable dt = op.QueryGrade(sql, orderby, count, 1);
    //    dt.Columns.Add("FormatGradeAsfTime");
    //    dt.Columns.Add("FKGradeUserName");
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        dt.Rows[i]["FormatGradeAsfTime"] = PowerCreator.LMS.Common.Utils.TimeUtils.FormatTime(dt.Rows[i]["GradeAsfTime"].ToString());
    //        dt.Rows[i]["FKGradeUserName"] = opuser.GetRealNameByUserID(int.Parse(dt.Rows[i]["FKGradeUserID"].ToString()));
    //    }

    //    lblCommentCount.Text = dt.Rows.Count.ToString();
    //    rptCommentList.DataSource = dt;
    //    rptCommentList.DataBind();


    //    //BinderCommentLevel();
    //}

    protected void rptCommentList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //if (e.CommandName.ToLower() == "mystate")
        //{
        //    int commentid = int.Parse(e.CommandArgument.ToString());
        //    PowerCreator.LMS.Comment.CommentInfo.CommentInfo cinfo = commentManager.LoadCommentInfo(commentid);
        //    if (cinfo != null)
        //    {
        //        cinfo.StateCount += 1;
        //        commentManager.UpdateCommentInfo(cinfo);
        //    }
        //}
    }

    //private void BinderCommentLevel()
    //{
    //    List<PowerCreator.LMS.Web.Comment.CommmentLevelInfo> list = PowerCreator.LMS.Web.Comment.CommentLevelType.GetCommentLevelList();
    //    for (int i = 0; i < list.Count; i++)
    //    {
    //        string search = string.Format(" ResourceID={0} and CommentLevel={1} ", ViewState["ResourceID"], ((PowerCreator.LMS.Web.Comment.CommmentLevelInfo)list[i]).Key.ToString());
    //        ((PowerCreator.LMS.Web.Comment.CommmentLevelInfo)list[i]).Count = opComment.QueryCommentCount(search).ToString();
    //    }
    //    rptCommentLevel.DataSource = list;
    //    rptCommentLevel.DataBind();

    //    string t1 = string.Format("ResourceID={0} and CommentLevel > 0", ViewState["ResourceID"]);
    //    Label lblCount = (Label)rptCommentLevel.FindControl("lblCount");
    //    if (lblCount != null)
    //    {
    //        lblCount.Text = opComment.QueryCommentCount(t1).ToString();
    //    }
    //}

    //private string GetTotalNum(string F_Position)
    //{
    //    string Rt = "0";
    //    if (F_Position.Length > 0)
    //    {
    //        string[] Arr_Position=F_Position.Split(':');
    //        if (Arr_Position.Length == 3)
    //        {
    //            Rt = (Convert.ToInt16(Arr_Position[0]) * 3600 + Convert.ToInt16(Arr_Position[1]) * 60 + Convert.ToInt16(Arr_Position[2])).ToString();
    //        }
    //    }
    //    return Rt;
    //}
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
}
